<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Pic_apply extends MY_Controller
{
	private $idtype;

	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
		$this->idtype = array('身份证', '护照', '通行证');
	}

	public function index()
	{
		$this->view('pic_apply');
	}

	public function data()
	{
		$page = ($this->input->post('page') ? intval($this->input->post('page')) : 1);
		$rows = ($this->input->post('rows') ? intval($this->input->post('rows')) : 10);
		$id = intval($this->input->post('id'));
		$status = intval($this->input->post('status'));
		$username = $this->input->post('username');
		$start = $this->input->post('start');
		$end = $this->input->post('end');
		$where[1] = 1;

		if ($username) {
			$user = $this->db->where('username', $username)->get('user')->row_array();
			$where['user_id'] = $user['id'];
		}

		if ($start) {
			$where['add_time>='] = strtotime($start);
		}

		if ($end) {
			$where['add_time<='] = strtotime($end);
		}

		if ($id) {
			$where['id'] = $id;
		}

		if ($id) {
			$where['id'] = $id;
		}

		if ((0 <= $status) && ($status != NULL)) {
			$where['status'] = $status;
		}

		$result['total'] = $this->db->where($where)->count_all_results('pic_apply');
		$start = ($page - 1) * $rows;
		$result['rows'] = $this->db->where($where)->limit($rows, $start)->get('pic_apply')->result_array();
		$idtype = array('身份证', '护照', '通行证');

		foreach ($result['rows'] as $k => $v) {
			$result['rows'][$k]['add_time'] = date('Y-m-d H:i', $v['add_time']);
			$result['rows'][$k]['idtype_name'] = $idtype[$v['idtype']];
		}

		echo json_encode($result);
	}

	public function edit($id)
	{
		$id = intval($id);
		$d = $this->db->where('id', $id)->get('pic_apply')->row_array();
		$idtype = array('身份证', '护照', '通行证');
		$d['idtype_name'] = $idtype[$d['idtype']];
		$this->load->view('pic_apply_edit', $d);
	}

	public function info($id)
	{
		$id = intval($id);
		$d = $this->db->where('id', $id)->get('pic_apply')->row_array();
		$d['add_time'] = date('Y-m-d H:i', $d['add_time']);
		$idtype = array('身份证', '护照', '通行证');
		$d['idtype_name'] = $idtype[$d['idtype']];
		$this->load->view('pic_apply_info', $d);
	}

	public function update()
	{
		$id = $this->input->post('id');
		$status = intval($this->input->post('status'));
		$info = $this->input->post('info');

		if (!in_array($status, array(1, 2))) {
			ajax_return(1, '修改失败');
		}

		$pic_apply = $this->db->where('id', $id)->get('pic_apply')->row_array();

		if (!$pic_apply) {
			ajax_return(1, '找不到要审核的对象');
		}

		if ($pic_apply['status'] == 1) {
			ajax_return(1, '已审核通过');
		}

		if ($status == 1) {
			$this->db->query('set autocommit=0');
			$this->db->query('lock tables t_user write, t_pic_apply write');
			$rs = array();
			$rs[] = $this->db->where('id', $id)->set('status', $status)->update('pic_apply');
			$rs[] = $this->db->where('id', $pic_apply['user_id'])->set('name', $pic_apply['name'])->set('idtype', $pic_apply['idtype'])->set('idno', $pic_apply['idno'])->set('area_code_idno', $pic_apply['area_code_idno'])->set('pic_front', $pic_apply['pic_front'])->set('pic_back', $pic_apply['pic_back'])->set('pic_hand', $pic_apply['pic_hand'])->update('user');

			if (correct($rs)) {
				$this->db->query('commit');
				$this->db->query('unlock tables');
				echo json_encode(array('msg' => '修改成功', 'error' => 0));
			}
			else {
				$this->db->query('rollback');
				$this->db->query('unlock tables');
				echo json_encode(array('msg' => '修改失败', 'error' => 1));
			}
		}
		else {
			$rs = $this->db->where('id', $id)->set('status', $status)->set('info', $info)->update('pic_apply');

			if ($rs) {
				ajax_return(0, '修改成功');
			}
			else {
				ajax_return(1, '修改失败');
			}
		}
	}

	public function del()
	{
		$id = $this->input->get('id');

		if ($id) {
			$rs = $this->db->where_in('id', $id)->delete('pic_apply');

			if ($rs) {
				echo json_encode(array('msg' => '删除成功', 'error' => 0));
			}
			else {
				echo json_encode(array('msg' => '删除失败', 'error' => 1));
			}
		}
		else {
			echo json_encode(array('msg' => '删除失败！至少选择一行数据', 'error' => 0));
		}
	}
}

?>
